/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.renren.modules.category.dto.TConsumerDTO;
import io.renren.modules.category.entity.*;
import io.renren.modules.category.service.TBrandService;
import io.renren.modules.category.service.TConsumerService;
import io.renren.modules.category.service.TInitialService;
import org.apache.commons.collections.CollectionUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RunWith(SpringRunner.class)
@SpringBootTest
public class CategoryTest809 {
    @Autowired
    private TConsumerService consumerService;
    @Autowired
    private TBrandService brandService;
    @Autowired
    private TInitialService initialService;
    @Test
    public void test(){
        Map<String, Object> params = new HashMap<>();
        //params.put("id",100003L);
        List<TConsumerEntity> tConsumerEntities = consumerService.selectList(params);
        System.out.println("===================");
        System.out.println(JSONUtil.toJsonStr(tConsumerEntities));
    }

    @Test
    public void test2(){
        TConsumerDTO consumerDTO = new TConsumerDTO();
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> brandParams;
        params.put("consumerId",100003L);
        List<TInitialEntity> initialList = initialService.selectList(params);
        System.out.println("===================");
        for(TInitialEntity intiial : initialList){
            brandParams = new HashMap<>();
            brandParams.put("initialId",intiial.getId());
            List<TBrandEntity> respBrandList = brandService.selectList(brandParams);
            intiial.setBrandList(respBrandList);
        }
        ObjectMapper mapper = new ObjectMapper();
        try {
            consumerDTO.setInitialList(initialList);
            consumerDTO.setName("女士");
            String json = mapper.writeValueAsString(consumerDTO);
            System.out.println(json);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }


    @Test
    public void buildJson(){
        Map<String, Object> consumerParams = new HashMap<>();
        Map<String, Object> initialParams;
        Map<String, Object> brandParams;
        List<TConsumerEntity> consumerListAll = consumerService.selectList(consumerParams);
        List<TInitialEntity> initialListNew;
        //品牌列表不为空的 品类集合
        Map<Long,List<TInitialEntity>> initialMap = new HashMap<>();
        //消费者分类：男士，女士
        for(TConsumerEntity consumer: consumerListAll){
            initialListNew = new ArrayList<>();
            initialParams = new HashMap<>();
            initialParams.put("code",consumer.getId());
            List<TInitialEntity> initialList = initialService.selectList(initialParams);
            //consumer.setInitialList(initialList);
            //首字母
            for(TInitialEntity intiial : initialList){
                brandParams = new HashMap<>();
                brandParams.put("initialId",intiial.getId());
                List<TBrandEntity> brandList = brandService.selectList(brandParams);
                intiial.setBrandList(brandList);
                if(intiial.getConsumerId().equals(consumer.getId()) && CollectionUtils.isNotEmpty(brandList)){
                    initialListNew.add(intiial);
                }
            }
            initialMap.put(consumer.getId(),initialListNew);
        }
        //重新构建
//        for(TConsumerEntity consumer: consumerListAll){
//            consumer.setInitialList(initialMap.get(consumer.getId()));
//        }
        ObjectMapper mapper = new ObjectMapper();
        try {
            String json = mapper.writeValueAsString(consumerListAll);
            System.out.println();
            System.out.println(json);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }






/**
 * [{"id":101,"name":"女士","sort":1,"initialList":[{"name":"热门品牌","consumerId":101,"sort":0,"brandList":[{"id":1006,"name":"热门品牌","initialId":10006,"consumerId":101,"sort":0}]},{"name":"A","consumerId":101,"sort":1,"brandList":[{"id":1007,"name":"Acne Studios","initialId":10007,"consumerId":101,"sort":1},{"id":1008,"name":"AEG","initialId":10007,"consumerId":101,"sort":1}]}]},{"id":102,"name":"男士","sort":2,"initialList":[{"name":"热门品牌","consumerId":102,"sort":0,"brandList":[{"id":1009,"name":"热门品牌","initialId":10011,"consumerId":102,"sort":0}]},{"name":"A","consumerId":102,"sort":1,"brandList":[{"id":1010,"name":"Acne Studios","initialId":10012,"consumerId":102,"sort":1}]},{"name":"B","consumerId":102,"sort":2,"brandList":[{"id":1012,"name":"BALLY","initialId":10013,"consumerId":102,"sort":1}]}]}]
 */
    /**
     * 解析Json 数据
     */
    @Test
    public void parseJson(){
        Long consumerId=102L;
        /**
         * 入参：
         * consumerId:101
         * collection: 1:只看收藏
         */
        String duccJson="[{\"id\":101,\"name\":\"女士\",\"sort\":1,\"initialList\":[{\"name\":\"热门品牌\",\"consumerId\":101,\"sort\":0,\"brandList\":[{\"id\":1006,\"name\":\"热门品牌\",\"initialId\":10006,\"consumerId\":101,\"sort\":0}]},{\"name\":\"A\",\"consumerId\":101,\"sort\":1,\"brandList\":[{\"id\":1007,\"name\":\"Acne Studios\",\"initialId\":10007,\"consumerId\":101,\"sort\":1},{\"id\":1008,\"name\":\"AEG\",\"initialId\":10007,\"consumerId\":101,\"sort\":1}]}]},{\"id\":102,\"name\":\"男士\",\"sort\":2,\"initialList\":[{\"name\":\"热门品牌\",\"consumerId\":102,\"sort\":0,\"brandList\":[{\"id\":1009,\"name\":\"热门品牌\",\"initialId\":10011,\"consumerId\":102,\"sort\":0}]},{\"name\":\"A\",\"consumerId\":102,\"sort\":1,\"brandList\":[{\"id\":1010,\"name\":\"Acne Studios\",\"initialId\":10012,\"consumerId\":102,\"sort\":1}]},{\"name\":\"B\",\"consumerId\":102,\"sort\":2,\"brandList\":[{\"id\":1012,\"name\":\"BALLY\",\"initialId\":10013,\"consumerId\":102,\"sort\":1}]}]}]\n";

        List<TConsumerEntity> consumerList = JSON.parseArray(duccJson, TConsumerEntity.class);
        ConsumerPO consumerPO = new ConsumerPO();
        List<InitialPO> initialPOList = new ArrayList<>();
        List<BrandPO> brandVOList;
        InitialPO initialPO;
        BrandVO brandVO;
        for(TConsumerEntity consumer : consumerList){
            if(!consumerId.equals(consumer.getId())){
                continue;
            }
            consumerPO.setName(consumer.getName());
            List<TInitialEntity> initials = consumer.getInitialList();
            for(TInitialEntity initial : initials){
                if(!initial.getConsumerId().equals(consumerId)){
                    continue;
                }
                brandVOList = new ArrayList<>();
                initialPO = new InitialPO();
                initialPO.setName(initial.getName());
//                    List<TBrandEntity> brandList = initial.getBrandList();
//                    for(TBrandEntity brandEntity: brandList){
//                        brandVO = new BrandVO();
//                        brandVO.setName(brandEntity.getName());
//                        brandVO.setCollection(0);
//                        brandVOList.add(brandVO);
//                    }
                //构建brandVOList
                buildBrandVOList(initial.getBrandList(),brandVOList);
                initialPO.setBrandList(brandVOList);
                initialPOList.add(initialPO);
            }
            consumerPO.setInitialList(initialPOList);
        }

        ObjectMapper mapper = new ObjectMapper();
        try {
            String json = mapper.writeValueAsString(consumerPO);
            System.out.println();
            System.out.println(json);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

    }

    private void buildBrandVOList(List<TBrandEntity> initialBrandList,List<BrandPO> brandVOList){
        List<TBrandEntity> brandList = initialBrandList;
        for(TBrandEntity brandEntity: brandList){
            BrandPO brandVO = new BrandPO();
            brandVO.setName(brandEntity.getName());
            brandVOList.add(brandVO);
        }
    }



    private Map<String,BrandVO> collectionMap(List<BrandVO> brandVOList){
        Map<String,BrandVO> brandVOMap = new HashMap<>();
        if(org.apache.commons.collections4.CollectionUtils.isEmpty(brandVOList)){
            return brandVOMap;
        }
        for (BrandVO brandVO : brandVOList){
            brandVOMap.put(brandVO.getName(),brandVO);
        }
        return brandVOMap;
    }


/**
 * [{"id":101,"name":"女士","sort":1,"initialList":[{"name":"热门品牌","consumerId":101,"sort":0,"brandList":[{"id":1006,"name":"热门品牌","initialId":10006,"consumerId":101,"sort":0}]},{"name":"A","consumerId":101,"sort":1,"brandList":[{"id":1007,"name":"Acne Studios","initialId":10007,"consumerId":101,"sort":1},{"id":1008,"name":"AEG","initialId":10007,"consumerId":101,"sort":1}]}]},{"id":102,"name":"男士","sort":2,"initialList":[{"name":"热门品牌","consumerId":102,"sort":0,"brandList":[{"id":1009,"name":"热门品牌","initialId":10011,"consumerId":102,"sort":0}]},{"name":"A","consumerId":102,"sort":1,"brandList":[{"id":1010,"name":"Acne Studios","initialId":10012,"consumerId":102,"sort":1}]},{"name":"B","consumerId":102,"sort":2,"brandList":[{"id":1012,"name":"BALLY","initialId":10013,"consumerId":102,"sort":1}]}]}]
 */
    /**
     * 解析Json 数据
     */
    @Test
    public void parseJson2(){
        Long consumerId=101L;
        Boolean collection=true;
        /**
         * 入参：
         * consumerId:101
         * collection: 1:只看收藏
         */
        String duccJson="[{\"id\":101,\"name\":\"女士\",\"sort\":1,\"initialList\":[{\"name\":\"热门品牌\",\"consumerId\":101,\"sort\":0,\"brandList\":[{\"id\":1006,\"name\":\"热门品牌\",\"initialId\":10006,\"consumerId\":101,\"sort\":0}]},{\"name\":\"A\",\"consumerId\":101,\"sort\":1,\"brandList\":[{\"id\":1007,\"name\":\"Acne Studios\",\"initialId\":10007,\"consumerId\":101,\"sort\":1},{\"id\":1008,\"name\":\"AEG\",\"initialId\":10007,\"consumerId\":101,\"sort\":1}]}]},{\"id\":102,\"name\":\"男士\",\"sort\":2,\"initialList\":[{\"name\":\"热门品牌\",\"consumerId\":102,\"sort\":0,\"brandList\":[{\"id\":1009,\"name\":\"热门品牌\",\"initialId\":10011,\"consumerId\":102,\"sort\":0}]},{\"name\":\"A\",\"consumerId\":102,\"sort\":1,\"brandList\":[{\"id\":1010,\"name\":\"Acne Studios\",\"initialId\":10012,\"consumerId\":102,\"sort\":1}]},{\"name\":\"B\",\"consumerId\":102,\"sort\":2,\"brandList\":[{\"id\":1012,\"name\":\"BALLY\",\"initialId\":10013,\"consumerId\":102,\"sort\":1}]}]}]\n";
        String collectJson="[{\"id\":1008,\"name\":\"AEG\"}]";

        List<BrandVO> brandVOList = JSON.parseArray(collectJson, BrandVO.class);
        Map<String, BrandVO> stringBrandVOMap = collectionMap(brandVOList);
        List<ConsumerPO> consumerList = JSON.parseArray(duccJson, ConsumerPO.class);
        ConsumerPO consumerPO = new ConsumerPO();
        List<InitialPO> initialPOList = new ArrayList<>();
        for(ConsumerPO consumer : consumerList){
            if(!consumerId.equals(consumer.getId())){
                continue;
            }
            consumerPO.setId(consumer.getId());
            consumerPO.setName(consumer.getName());
            consumerPO.setSort(consumer.getSort());
            List<InitialPO> initials = consumer.getInitialList();
            for(InitialPO initial : initials){
                if(!initial.getConsumerId().equals(consumerId)){
                    continue;
                }

                for(BrandPO brandPO:initial.getBrandList()){
                    brandPO.setCollection(stringBrandVOMap.containsKey(brandPO.getName()));
                }
                initialPOList.add(initial);
            }
            consumerPO.setInitialList(initialPOList);
        }

        ObjectMapper mapper = new ObjectMapper();
        try {
            String json = mapper.writeValueAsString(consumerPO);
            System.out.println();
            System.out.println(json);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

    }




/**
 * [{"id":101,"name":"女士","sort":1,"initialList":[{"name":"热门品牌","consumerId":101,"sort":0,"brandList":[{"id":1006,"name":"热门品牌","initialId":10006,"consumerId":101,"sort":0}]},{"name":"A","consumerId":101,"sort":1,"brandList":[{"id":1007,"name":"Acne Studios","initialId":10007,"consumerId":101,"sort":1},{"id":1008,"name":"AEG","initialId":10007,"consumerId":101,"sort":1}]}]},{"id":102,"name":"男士","sort":2,"initialList":[{"name":"热门品牌","consumerId":102,"sort":0,"brandList":[{"id":1009,"name":"热门品牌","initialId":10011,"consumerId":102,"sort":0}]},{"name":"A","consumerId":102,"sort":1,"brandList":[{"id":1010,"name":"Acne Studios","initialId":10012,"consumerId":102,"sort":1}]},{"name":"B","consumerId":102,"sort":2,"brandList":[{"id":1012,"name":"BALLY","initialId":10013,"consumerId":102,"sort":1}]}]}]
 */
    /**
     * 解析Json 数据
     */
    @Test
    public void parseJsonUp(){
        /**
         * 入参：
         * consumerId:101
         * collection: 1:只看收藏
         */
        Long consumerId=101L;
        Boolean collection=false;

        String duccJson="[{\"id\":101,\"name\":\"女士\",\"sort\":1,\"initialList\":[{\"name\":\"热门品牌\",\"consumerId\":101,\"sort\":0,\"brandList\":[{\"id\":1006,\"name\":\"热门品牌\",\"initialId\":10006,\"consumerId\":101,\"sort\":0}]},{\"name\":\"A\",\"consumerId\":101,\"sort\":1,\"brandList\":[{\"id\":1007,\"name\":\"Acne Studios\",\"initialId\":10007,\"consumerId\":101,\"sort\":1},{\"id\":1008,\"name\":\"AEG\",\"initialId\":10007,\"consumerId\":101,\"sort\":1}]}]},{\"id\":102,\"name\":\"男士\",\"sort\":2,\"initialList\":[{\"name\":\"热门品牌\",\"consumerId\":102,\"sort\":0,\"brandList\":[{\"id\":1009,\"name\":\"热门品牌\",\"initialId\":10011,\"consumerId\":102,\"sort\":0}]},{\"name\":\"A\",\"consumerId\":102,\"sort\":1,\"brandList\":[{\"id\":1010,\"name\":\"Acne Studios\",\"initialId\":10012,\"consumerId\":102,\"sort\":1}]},{\"name\":\"B\",\"consumerId\":102,\"sort\":2,\"brandList\":[{\"id\":1012,\"name\":\"BALLY\",\"initialId\":10013,\"consumerId\":102,\"sort\":1}]}]}]\n";
        String collectJson="[{\"id\":1008,\"name\":\"AEG\"},{\"id\":1006,\"name\":\"热门品牌\"}]";

        List<BrandVO> brandVOList = JSON.parseArray(collectJson, BrandVO.class);
        Map<String, BrandVO> stringBrandVOMap = collectionMap(brandVOList);
        List<ConsumerPO> consumerList = JSON.parseArray(duccJson, ConsumerPO.class);
        //出参 最外层
        ConsumerPO consumerPO = new ConsumerPO();
        List<InitialPO> initialPOList = new ArrayList<>();
        //新的分类，过滤只看收藏的内容
        Map<Long,List<InitialPO>> initialMap = new HashMap<>();
        //收藏的集合
        List<InitialPO> initialCollectionList;
        List<BrandPO> brandCollectionVOList = null;
        InitialPO initialCollectionVO =null;
        for(ConsumerPO consumer : consumerList){
            if(!consumerId.equals(consumer.getId())){
                continue;
            }
            initialCollectionList = new ArrayList<>();
            List<InitialPO> initials = consumer.getInitialList();
            for(InitialPO initial : initials){
                if(!initial.getConsumerId().equals(consumerId)){
                    continue;
                }
                //只看收藏的时候，不返回热门品牌分类
                if(collection && "热门品牌".equals(initial.getName())){
                    continue;
                }
                for(BrandPO brandVO:initial.getBrandList()){
                    brandCollectionVOList = new ArrayList<>();
                    boolean isCollection =stringBrandVOMap.containsKey(brandVO.getName());
                    brandVO.setCollection(isCollection);
                    if(collection && isCollection){
                        brandCollectionVOList.add(brandVO);
                    }
                }
                if(collection && org.apache.commons.collections4.CollectionUtils.isNotEmpty(brandCollectionVOList)){
                    initialCollectionVO = new InitialPO();
                    initialCollectionVO.setName(initial.getName());
                    initialCollectionVO.setBrandList(brandCollectionVOList);
                    initialCollectionVO.setSort(initial.getSort());
                    initialCollectionList.add(initialCollectionVO);
                }else{
                    initialPOList.add(initial);
                }
            }
            if(collection){
                //收藏的 数据
                initialMap.put(consumer.getId(),initialCollectionList);
            }else{
                //全部 数据
                initialMap.put(consumer.getId(), initialPOList);
            }
            // consumerPO.setInitialList(initialPOList);
        }

        //重新构建
        for(ConsumerPO consumer : consumerList){
            if(!consumerId.equals(consumer.getId())){
                continue;
            }
            consumerPO.setId(consumer.getId());
            consumerPO.setName(consumer.getName());
            consumerPO.setSort(consumer.getSort());
            consumerPO.setInitialList(initialMap.get(consumer.getId()));
        }

        ObjectMapper mapper = new ObjectMapper();
        try {
            String json = mapper.writeValueAsString(consumerPO);
            System.out.println();
            System.out.println(json);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

    }



    /**
     *收藏的集合转换为map
     */
    private Map<String,BrandVO> getCollectionMap(List<BrandVO> brandVOList){
        if(org.apache.commons.collections4.CollectionUtils.isEmpty(brandVOList)){
            return new HashMap<>();
        }
        Map<String, BrandVO> brandVOMap = brandVOList.stream()
                .collect(Collectors.toMap(BrandVO::getName, brandVO -> brandVO));
        return brandVOMap;
    }

    @Test
    public void testMap(){
        List<BrandVO> brandVOList = null;
//        BrandVO brandVO = new BrandVO();
//        brandVO.setName("11111");
//        brandVOList.add(brandVO);
        //{"11111":{"name":"11111"}}
        Map<String, BrandVO> stringBrandVOMap = getCollectionMap(brandVOList);
        System.out.println(JSON.toJSONString(stringBrandVOMap));
        System.out.println(stringBrandVOMap.get("111"));
    }


}
